const router = require('koa-router')()
const db = require('../db/initdb')
const fs = require('fs');
const path = require('path');
const { koaBody } = require('koa-body');

//获取数据
router.get('/subscriber', async (ctx, next) => {
  let currentPage = ctx.query.currentPage;
  let sql1 = 'select count(id) from hs.user'
  let result1 = await db.query(sql1);
  let sql = `select * from hs.user order by relogin desc limit 10 offset (${currentPage - 1}*10)`
  let result = await db.query(sql);
  if (result.rowCount > 0) {
    let pageList = result.rows;
    ctx.body = {
      "status": 200,
      "pageList": pageList,
      "total": result1.rows[0].count
    };

  }
})
// 修改资料
router.get('/editsubscriber', async (ctx, next) => {
  let id=ctx.query.id
  let name=ctx.query.name
  let intro=ctx.query.intro
  let avatar=ctx.query.avatar
  let sql = `update hs.user set name=$1,avatar=$2,intro=$3 where id=$4`
  let result = await db.query(sql,[name,avatar,intro,id]);
  if (result.rowCount > 0) {
    ctx.body = {
      "status": 200
    };
  }
})

// 上传头像
router.post('/subscriber/addsubscriber/image', koaBody(), async (ctx, next) => {
  let filename = (ctx.request.files.file.newFilename);
  ctx.body = filename
})
//修改头像
router.post('/subscriber/editsubscriber/image', koaBody(), async (ctx, next) => {
  let id = ctx.query.id;
  let filename = (ctx.request.files.file.newFilename);
  let sql = 'UPDATE hs.user set avatar=$1 where id=$2 returning avatar';
  let result = await db.query(sql, [filename, id]);
  ctx.body = result.rows[0];
})

//添加用户
router.get('/addSubscriber', async (ctx, next) => {
  let relogin=new Date().toISOString()
  let id = String(Date.now()).substring(7)
  let name=ctx.query.name
  let sex=ctx.query.sex
  let intro=ctx.query.intro
  let avatar=ctx.query.avatar
  let sql = `insert into hs.user (id,name,avatar,concernnum,likenum,fannum,usertype,relogin,intro,sex) values($1,$2,$3,0,0,0,0,$4,$5,$6)`
  let result = await db.query(sql,[id,name,avatar,relogin,intro,sex]);
  if (result.rowCount > 0) {
    ctx.body = {
      "status": 200
    };
  }
})

//删除用户
router.get('/delsubscriber', async (ctx, next) => {
  let id=ctx.query.id
  let newSelectedRowKeys=ctx.request.newSelectedRowKeys
  console.log(newSelectedRowKeys)
  let sql="delete from hs.user where id=$1"
  let result = await db.query(sql,[id]);
  if (result.rowCount > 0) {
    ctx.body = {
      "status": 200
    };
  }
})


//搜索
router.get('/searchSubscriber', async (ctx, next) => {
  let currentPage = ctx.query.currentPage;
  let searchData=ctx.query.searchData
  let sql1 = `select count(id) from hs.user where name like '%${searchData}%'`
  let result1= await db.query(sql1);
  let sql = `select * from hs.user where name like '%${searchData}%' order by relogin desc limit 10 offset (${currentPage - 1}*10)`
  let result = await db.query(sql);
  let pageList =result.rows;
  console.log(pageList)
  ctx.body = {
    "status":200,
    "pageList":pageList,
    "total":result1.rows[0].count
  };
})
//头像显示
router.get("/avatar/image/:name", async (ctx, next) => {
  let filePath = path.join(__dirname, `../public/images/${ctx.params.name}`);
  let fileContent = fs.readFileSync(filePath)
  ctx.set("Content-Type", "image/png")
  ctx.body = fileContent;
})


module.exports = router